package com.android.launcher3.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.launcher3.AppInfo;
import com.android.launcher3.Launcher;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;

import java.util.List;

public enum AddAppPopWindow {

    INSTANT;

    private final static int MAX_ROW_COUNT = 7;

    private boolean isShow;

    public void show(@NonNull Launcher launcher, List<AppInfo> apps, ChooseAppListener chooseAppListener) {

        if (apps == null || apps.isEmpty() || isShow) return;

        DisplayMetrics dm = launcher.getResources().getDisplayMetrics();
        PopupWindow popupWindow = new PopupWindow(launcher);
        View container = LayoutInflater.from(launcher).inflate(R.layout.add_widget_pop_window_half, null);
        RecyclerView recyclerView = container.findViewById(R.id.recycler);
        int rowNum = dm.widthPixels / (Utilities.pxFromDp(100, dm));

        if (rowNum > MAX_ROW_COUNT) {
            rowNum = MAX_ROW_COUNT;
        }

        GridLayoutManager gridLayoutManager = new GridLayoutManager(launcher, 2, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(new AppAdapter(launcher, apps, position -> {
            if (chooseAppListener != null) {
                chooseAppListener.onChoose(apps.get(position));
            }
            popupWindow.dismiss();
        }));
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, int itemPosition, @NonNull RecyclerView parent) {

                outRect.left = Utilities.pxFromDp(30, dm);
                outRect.right = Utilities.pxFromDp(30, dm);
                outRect.bottom = Utilities.pxFromDp(0, dm);
                outRect.top = Utilities.pxFromDp(0, dm);
            }

            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.left = Utilities.pxFromDp(30, dm);
                outRect.right = Utilities.pxFromDp(30, dm);
                outRect.bottom = Utilities.pxFromDp(0, dm);
                outRect.top = Utilities.pxFromDp(0, dm);
            }
        });
        popupWindow.setContentView(container);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(dm.heightPixels / 2);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(false);
        popupWindow.showAsDropDown(launcher.findViewById(R.id.custom_hot_seat));
        popupWindow.setOnDismissListener(() -> isShow = false);
        isShow = true;

    }

    public static class AppAdapter extends RecyclerView.Adapter<AppAdapter.VH> {

        public AppAdapter(Context context, List<AppInfo> data, OnItemClickListener onItemClickListener) {
            this.context = context;
            this.data = data;
            this.onItemClickListener = onItemClickListener;
        }

        private Context context;
        private List<AppInfo> data;
        private OnItemClickListener onItemClickListener;


        @NonNull
        @Override
        public AppAdapter.VH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(context).inflate(R.layout.layout_add_app, null);
            return new AppAdapter.VH(view);
        }

        @Override
        public void onBindViewHolder(@NonNull VH vh, int i) {
            vh.imageView.setImageBitmap(data.get(i).iconBitmap);
            vh.textView.setText(Utilities.trim(data.get(i).title));
            vh.itemView.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(i);
                }
            });
        }

        @Override
        public int getItemCount() {
            return data == null ? 0 : data.size();
        }

        public static class VH extends RecyclerView.ViewHolder {

            ImageView imageView;
            TextView textView;

            public VH(@NonNull View itemView) {
                super(itemView);
                imageView = itemView.findViewById(R.id.image);
                textView = itemView.findViewById(R.id.text);
            }
        }

        public interface OnItemClickListener {
            void onItemClick(int position);
        }
    }


    public interface ChooseAppListener {
        void onChoose(AppInfo appInfo);
    }

}
